/*
 * @Author: your name
 * @Date: 2022-03-26 22:17:37
 * @LastEditTime: 2022-03-27 23:27:53
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \drivingTow\src\practice\practice.service.ts
 */
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Trainee } from 'src/trainees/entities/trainee.entity';
import { getRepository, Repository } from 'typeorm';
import { CreatePracticeDto, PracticeInfoDto, PracticeRo } from './dto/create-practice.dto';
import { UpdatePracticeDto } from './dto/update-practice.dto';
import { Practice } from './entities/practice.entity';

@Injectable()
export class PracticeService {
  constructor(
    @InjectRepository(Practice)
    private readonly postsRepository: Repository<Practice>,
  ){}

  // 创建教练训练表
  async createCoach(post:CreatePracticeDto,user):Promise<any>{
    const qb = await this.postsRepository
      .createQueryBuilder('practice')
      .where('practice.coach =:coach ',{coach:user.id})
      .andWhere('practice.create_time =:create_time',{create_time:post.create_time})
      .getOne()
    if(qb){
      return '今日已安排练车'
    }else{
      const postParam:Partial<Practice> = {
        ...post,
        coachName:user.username,
        coach:user.id,
        telephone:user.number
      }
      const newPost:Practice = await this.postsRepository.create({
        ...postParam
      });
      const created = await this.postsRepository.save(newPost)
      return '创建成功'
    }
    
  }

  // 学员约车
  async updataPractice(post):Promise<any>{
    let { userId,create_time } = post
    const userRepository = getRepository(Trainee);
    const trainee = await userRepository.createQueryBuilder('Trainee')
      .where('Trainee.trainee =:userId',{userId:userId})
      .andWhere('Trainee.create_time =:create_time',{create_time:create_time})
      .getOne()
    if(trainee){
      return "学员已约车"
    }else{
      const qb = this.postsRepository
        .createQueryBuilder('practice')
        .where('practice.coach =:coach ',{coach:post.coach})
        .andWhere('practice.create_time =:create_time',{create_time:post.create_time})
      const result = await qb.getOne()
      let str = String(post.userId)
      if(post.Eight !== '-'){
        await this.postsRepository.update(result.id,{Eight:result.Eight+str+','})
      }else if(post.ten !== '-'){
        await this.postsRepository.update(result.id,{ten:result.ten+str+','})
      }else if(post.twelve !== '-'){
        await this.postsRepository.update(result.id,{twelve:result.twelve+str+','})
      }else if(post.fourteen !== '-'){
        await this.postsRepository.update(result.id,{fourteen:result.fourteen+str+','})
      }else if(post.sixteen !== '-'){
        await this.postsRepository.update(result.id,{sixteen:result.sixteen+str+','})
      }else{
        return 'error'
      }
      return 'success'
    }
    
  }

  // 返回练车表各个时间段还有多少位置
  async getAnyTime(post):Promise<any>{
    const qb = this.postsRepository
    .createQueryBuilder('practice')
    .where('practice.coach =:coach ',{coach:post.coach})
    .andWhere('practice.create_time =:create_time',{create_time:post.create_time})
    const result = await qb.getOne()
    let EightCount = 3
    let tenCount = 3
    let twelveCount = 3
    let fourteenCount = 3
    let sixteenCount = 3
    if(result.Eight){
      let EightStr = result.Eight
      let EightStrLen = result.Eight.length
      let newEightStr = EightStr.replace(',','')
      EightCount = 3 - EightStrLen + newEightStr.length
    }
    if(result.ten){
      let tenStr = result.ten
      let tenStrLen = result.ten.length
      let newtenStr = tenStr.replace(',','')
      tenCount = 3 - tenStrLen + newtenStr.length
    }
    if(result.twelve){
      let twelveStr = result.twelve
      let twelveStrLen = result.twelve.length
      let newtwelveStr = twelveStr.replace(',','')
      twelveCount =3 - twelveStrLen + newtwelveStr.length
    }
    if(result.fourteen){
      let fourteenStr = result.fourteen
      let fourteenStrLen = result.fourteen.length
      let newfourteenStr = fourteenStr.replace(',','')
      fourteenCount =3 - fourteenStrLen + newfourteenStr.length
    }
    if(result.sixteen){
      let sixteenStr = result.sixteen
      let sixteenStrLen = result.sixteen.length
      let newsixteenStr = sixteenStr.replace(',','')
      sixteenCount =3 - sixteenStrLen + newsixteenStr.length
    }
    return {
      EightCount :EightCount,
      tenCount :tenCount,
      twelveCount :twelveCount,
      fourteenCount :fourteenCount,
      sixteenCount :sixteenCount,
    }
  }

  // 教练员查看今日练车表
  async getTheDay(post):Promise<any>{
    const qb = await this.postsRepository
      .createQueryBuilder('practice')
      .where('practice.create_time =:create_time',{ create_time:post.create_time})
      .andWhere('practice.coach =:coach',{coach:post.userId})
    let practice = await qb.getOne()
    return practice
  }

  // 教练员查看全部练车记录
  async getAllPractice(post):Promise<PracticeRo>{
    const qb = this.postsRepository
      .createQueryBuilder('practice')
      .where('practice.coach =:coach',{coach:post.userId})
      .orderBy('practice.id','DESC')
    const Total = await qb.getCount()
    const { pageSize,pageNum} = post
    qb.limit(pageSize)
    qb.offset(pageSize * (pageNum - 1))
    let practice = await qb.getMany()
    const result:PracticeInfoDto[] = practice.map((item) => item.toResponseObject())
    return {list:result,Total:Total}
  }

  // 管理员查看所有的练车表
  async getMenegerPractice(post):Promise<PracticeRo>{
    const { pageSize,pageNum,create_time} = post
    if(create_time){
      const qb = this.postsRepository
      .createQueryBuilder('practice')
      .orderBy('practice.id','DESC')
      .where('practice.catogary = :catogary',{catogary:post.catogary})
      .andWhere('practice.create_time = :create_time',{create_time:post.create_time})
      const Total = await qb.getCount()
      qb.limit(pageSize)
      qb.offset(pageSize * (pageNum - 1))
      let practice = await qb.getMany()
      const result:PracticeInfoDto[] = practice.map((item) => item.toResponseObject())
      return {list:result,Total:Total}
    }else{
      const qb = this.postsRepository
      .createQueryBuilder('practice')
      .orderBy('practice.id','DESC')
      const Total = await qb.getCount()
      qb.limit(pageSize)
      qb.offset(pageSize * (pageNum - 1))
      let practice = await qb.getMany()
      const result:PracticeInfoDto[] = practice.map((item) => item.toResponseObject())
      return {list:result,Total:Total}
    }
  }

  // 取消约车
  async CancelProctice(post):Promise<any>{
    const qb = this.postsRepository
      .createQueryBuilder('practice')
      .where('practice.coach =:coach',{coach:post.coach})
      .andWhere('practice.create_time =:create_time',{create_time:post.create_time})
    const result = await qb.getOne()
    let str = String(post.userId)+','
    if(result.Eight.includes(str)){
      await this.postsRepository.update(result.id,{Eight:result.Eight.replace(str,'')})
    }else if(result.ten.includes(str)){
      await this.postsRepository.update(result.id,{ten:result.ten.replace(str,'')})
    }else if(result.twelve.includes(str)){
      await this.postsRepository.update(result.id,{twelve:result.twelve.replace(str,'')})
    }else if(result.fourteen.includes(str)){
      await this.postsRepository.update(result.id,{fourteen:result.fourteen.replace(str,'')})
    }else if(result.sixteen.includes(str)){
      await this.postsRepository.update(result.id,{sixteen:result.sixteen.replace(str,'')})
    }else{
      return 'error'
    }
    return 'success'
  }

  // 根据Id查找练车表
  async findPracticeById(id):Promise<any>{
    return await this.postsRepository.findOne(id)
  }
}
